package com.example.administrator.teainfo.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.example.administrator.teainfo.R;
import com.example.administrator.teainfo.adapter.MainPagerAdapter;
import com.example.administrator.teainfo.fragment.ContentFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        tabLayout = (TabLayout) findViewById(R.id.activity_main_tl);
        viewPager = (ViewPager) findViewById(R.id.activity_main_vp);
        list = new ArrayList<>();
        tabLayout.setTabTextColors(Color.GRAY,Color.GREEN);
        //设置小滑块的高度
        tabLayout.setSelectedTabIndicatorHeight(3);
        //设置小滑块的颜色
        tabLayout.setSelectedTabIndicatorColor(Color.GREEN);
        //设置顶部导航的背景色
        tabLayout.setBackgroundColor(Color.parseColor("#EBEBEB"));
        final String[] arr = getResources().getStringArray(R.array.types);
        for (int i = 0; i < arr.length; i++) {
            ContentFragment fragment = new ContentFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("type",i);
            fragment.setArguments(bundle);
            list.add(fragment);
        }
        MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager(),list,arr);
        viewPager.setAdapter(adapter);
        //设置关联的ViewPager对象
        tabLayout.setupWithViewPager(viewPager);
        TabLayout.Tab tb= tabLayout.newTab();
        tb.setIcon(R.drawable.more);
        tb.setTag(1);
        tabLayout.addTab(tb);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(tab.getPosition()!=arr.length)
                {
                    viewPager.setCurrentItem(tab.getPosition(),true);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
